import { defHttp } from '/@/utils/http/axios';
import { Modal } from 'ant-design-vue';
import { useSuccess } from "/@/hooks/rw/handleSuccess";
const { rhandleSuccess } = useSuccess();
enum Api {
	list = 'Roles',
	listByTenant = '/sys/role/listByTenant',
	save = 'Roles',
	edit = 'Roles',
	deleteRole = 'Roles',
	deleteBatch = '/sys/role/deleteBatch',
	exportXls = '/sys/role/exportXls',
	importExcel = '/sys/role/importExcel',
	isRoleExist = '/sys/role/checkRoleCode',
	queryTreeListForRole = 'Menus/RoleMenuTrees',
	queryRolePermission = 'Menus/RoleMenuTrees',
	saveRolePermission = 'Roles/Resource',
	saveOrganizationPermission = 'api/RoleOrganization/save',
	queryDataRule = '/sys/role/datarule',
	getOrganizationTrees = "api/Organization/all",
	getParentDesignList = '/act/process/extActDesignFlowData/getDesFormFlows',
	saveRoleDesign = '/joa/designform/designFormCommuse/sysRoleDesignAdd',
	userList = '/sys/user/userRoleList',
	deleteUserRole = '/sys/user/deleteUserRole',
	batchDeleteUserRole = '/sys/user/deleteUserRoleBatch',
	addUserRole = '/sys/user/addSysUserRole',
	saveRoleIndex = '/sys/sysRoleIndex/add',
	editRoleIndex = '/sys/sysRoleIndex/edit',
	queryIndexByCode = '/sys/sysRoleIndex/queryByCode',
}
/**
 * 导出api
 */
export const getExportUrl = Api.exportXls;
/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;
/**
 * 系统角色列表
 * @param params
 */
export const list = (params) => defHttp.get({ url: Api.list, params });
/**
 * 角色病区权限tree
 * @param params
 */
export const getOrganizationTrees = () => defHttp.get({ url: Api.getOrganizationTrees });
/**
 * 租户角色列表
 * @param params
 */
export const listByTenant = (params) => defHttp.get({ url: Api.listByTenant, params });

/**
 * 删除角色
 */
export const deleteRole = (id, handleSuccess) => {
	if (!Array.isArray(id)) {
		id = [id]
	}
	return defHttp.delete({ url: Api.deleteRole, data: id }).then((res) => {
		rhandleSuccess(res, handleSuccess)
	});
};
/**
 * 批量删除角色
 * @param params
 */
export const batchDeleteRole = (params, handleSuccess) => {
	Modal.confirm({
		title: '确认删除',
		content: '是否删除选中数据',
		okText: '确认',
		cancelText: '取消',
		onOk: () => {
			return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
				handleSuccess();
			});
		},
	});
};
/**
 * 保存或者更新角色
 * @param params
 */
export const saveOrUpdateRole = (data, isUpdate) => {
	if (isUpdate) return defHttp.put({ url: Api.edit + '/' + data.id, data })
	else return defHttp.post({ url: Api.save, data })
};
/**
 * 编码校验
 * @param params
 */
export const isRoleExist = (params) => defHttp.get({ url: Api.isRoleExist, params }, { isTransformResponse: false });
/**
 * 根据角色查询树信息
 */
export const queryTreeListForRole = () => defHttp.get({ url: Api.queryTreeListForRole });
/**
 * 查询角色权限
 */
export const queryRolePermission = (id) => defHttp.get({ url: `Roles/${id}/resourceIds` });
/**
 * 查询角色病区权限
 */
export const queryOrganizationPermission = (id) => defHttp.get({ url: `api/RoleOrganization/roleId/${id}` });
/**
 * 保存角色权限
 */
export const saveRolePermission = (data) => defHttp.put({ url: Api.saveRolePermission, data });
/**
 * 保存页面权限
 */
export const saveOrganizationPermission = (data) => defHttp.post({ url: Api.saveOrganizationPermission, data });
/**
 * 查询角色数据规则
 */
export const queryDataRule = (params) =>
	defHttp.get({ url: `${Api.queryDataRule}/${params.functionId}/${params.roleId}` }, { isTransformResponse: false });
/**
 * 保存角色数据规则
 */
export const saveDataRule = (params) => defHttp.post({ url: Api.queryDataRule, params });
/**
 * 获取表单数据
 * @return List<Map>
 */
export const getParentDesignList = () => defHttp.get({ url: Api.getParentDesignList });
/**
 * 获取角色表单数据
 * @return List<Map>
 */
export const getRoleDegisnList = (params) => defHttp.get({ url: Api.getRoleDegisnList, params });
/**
 * 提交角色工单信息
 */
export const saveRoleDesign = (params) => defHttp.post({ url: Api.saveRoleDesign, params });
/**
 * 角色列表接口
 * @param params
 */
export const userList = (params) => defHttp.get({ url: Api.userList, params });
/**
 * 删除角色用户
 */
export const deleteUserRole = (params, handleSuccess) => {
	return defHttp.delete({ url: Api.deleteUserRole, params }, { joinParamsToUrl: true }).then(() => {
		handleSuccess();
	});
};
/**
 * 批量删除角色用户
 * @param params
 */
export const batchDeleteUserRole = (params, handleSuccess) => {
	Modal.confirm({
		title: '确认删除',
		content: '是否删除选中数据',
		okText: '确认',
		cancelText: '取消',
		onOk: () => {
			return defHttp.delete({ url: Api.batchDeleteUserRole, params }, { joinParamsToUrl: true }).then(() => {
				handleSuccess();
			});
		},
	});
};
/**
 * 添加已有用户
 */
export const addUserRole = (params, handleSuccess) => {
	return defHttp.post({ url: Api.addUserRole, params }).then(() => {
		handleSuccess();
	});
};
/**
 * 保存或者更新
 * @param params
 * @param isUpdate 是否是更新数据
 */
export const saveOrUpdateRoleIndex = (params, isUpdate) => {
	let url = isUpdate ? Api.editRoleIndex : Api.saveRoleIndex;
	return defHttp.post({ url: url, params });
};
/**
 * 根据code查询首页配置
 * @param params
 */
export const queryIndexByCode = (params) => defHttp.get({ url: Api.queryIndexByCode, params }, { isTransformResponse: false });
